package com.itc.bpm.framework.listener;

import com.google.common.collect.ImmutableSet;
import com.itc.bpm.bean.BpmProcessInstanceExt;
import com.itc.bpm.service.BpmProcessInstanceExtService;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.engine.delegate.event.AbstractFlowableEngineEventListener;
import org.flowable.engine.delegate.event.FlowableCancelledEvent;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * 流程实例监听处理
 * @author yl
 */
@Component
@Slf4j
public class BpmProcessInstanceEventListener extends AbstractFlowableEngineEventListener {

    @Autowired
    BpmProcessInstanceExtService bpmProcessInstanceExtService;
    public static final Set<FlowableEngineEventType> PROCESS_INSTANCE_EVENTS = ImmutableSet.<FlowableEngineEventType>builder()
            .add(FlowableEngineEventType.PROCESS_CANCELLED)
            .add(FlowableEngineEventType.PROCESS_COMPLETED)
            .build();

    public BpmProcessInstanceEventListener(){
        super(PROCESS_INSTANCE_EVENTS);
    }
    @Override
    protected void processCancelled(FlowableCancelledEvent event) {
    }

    @Override
    protected void processCompleted(FlowableEngineEntityEvent event) {
        // 完成任务，修改任务状态
        bpmProcessInstanceExtService.updateStatus(event.getProcessInstanceId(),1);
        // 发送通知消息
    }
}
